How to get current year in Ruby?

by porter.bins , in category: Ruby , 2 years ago

How to get current year in Ruby?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by jerod.sauer , 2 years ago

@porter.bins you can actually use DateTime object to get the current year in Ruby, code:


1
2
3
4
5
6
require 'date_core'

year = DateTime.now.year

# Output: 2022
print(year)


Member

by tina , a year ago

@porter.bins 

You can get the current year in Ruby using the Time class as follows:

1
current_year = Time.now.year


This will return the current year as an integer.